package com.huangkailong.leetcode;

/**
 * 给你一个字符串 s，找到 s 中最长的回文子串。<br/>
 *
 * 示例 1：<br/>
 * 输入：s = "babad"<br/>
 * 输出："bab"<br/>
 * 解释："aba" 同样是符合题意的答案。<br/>
 *
 * 示例 2：<br/>
 * 输入：s = "cbbd"<br/>
 * 输出："bb"<br/>
 *
 * 示例 3：<br/>
 * 输入：s = "a"<br/>
 * 输出："a"<br/>
 *
 * 示例 4：<br/>
 * 输入：s = "ac"<br/>
 * 输出："a"<br/>
 *
 * 提示：<br/>
 * 1 <= s.length <= 1000<br/>
 * s 仅由数字和英文字母（大写和/或小写）组成<br/>
 *
 * @author huangkl
 * @since 1.0.0
 */
public class LongestPalindrome {

    /**
     * 暴力枚举
     *
     * 时间复杂度：O(n^3)
     */
    public static String solution1(String s){
        if(s.length() == 0){
            return "";
        }
        int maxLen = 1;
        int ll = 0;
        int rr = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i; j < s.length(); j++) {
                // 子串长度小于 maxLen , 直接跳过判断
                int len = j - i + 1;
                if(len <= maxLen){
                    continue;
                }

                int l = i;
                int r = j;
                while (l <= r){
                    if(s.charAt(l) != s.charAt(r)){
                        break;
                    }
                    l++;
                    r--;
                }
                if(l > r){
                    // 子串是回文串
                    maxLen = len;
                    ll = i;
                    rr = j;
                }
            }
        }
        return  s.substring(ll, rr+1);
    }

    /**
     * 中心扩散
     *
     * 时间复杂度：O(n^2)
     */
    public static String solution2(String s){
        if(s.length() == 0){
            return "";
        }
        int maxLen = 1;
        int ll = 0;
        int rr = 0;
        for (int i = 0; i < s.length(); i++) {
            // 奇数情况
            int l = i-1, r = i+1;
            while (l >= 0 && r < s.length()){
                if(s.charAt(l) != s.charAt(r)){
                    break;
                }
                int len = r - l + 1;
                if(maxLen < len){
                    maxLen = len;
                    ll = l;
                    rr = r;
                }
                l--;
                r++;
            }
            // 偶数情况
            l = i;
            r = i+1;
            while (l >= 0 && r < s.length()){
                if(s.charAt(l) != s.charAt(r)){
                    break;
                }
                int len = r - l + 1;
                if(maxLen < len){
                    maxLen = len;
                    ll = l;
                    rr = r;
                }
                l--;
                r++;
            }
        }
        return  s.substring(ll, rr+1);
    }

    /**
     * 动态规划
     *
     * 时间复杂度：O(n^2)
     *
     *
     *
     * 状态方程：dp(i, j) = (((s[i] == s[j]) && db(i+1, j-1)))
     * 1.dp(i, j) 表示 s[i..j] 是否为回文串
     * 2.(j-1)-(i+1)+1 > 2 --> 只有 len > 2 时才需要状态方式进行运算
     * 3.只需要判断 i >= j 的情况
     * 4.dp(i, j) = true 当 i == j
     */
    public static String solution3(String s){
        int length = s.length();
        boolean[][] dp = new boolean[length][length];

        // 初始化数据
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                dp[i][j] = false;
            }
        }

        int ll = 0;
        int rr = 0;
        int maxLen = 1;
        for (int j = 0; j < length; j++) {
            for (int i = 0; i <= j; i++) {
                int len = j-i+1;
                if(len == 1){
                    dp[i][j] = true;
                }else {
                    boolean eq = s.charAt(i) == s.charAt(j);
                    if(len == 2){
                        if(eq){
                            dp[i][j] = true;
                            if(len > maxLen){
                                ll = i;
                                rr = j;
                                maxLen = len;
                            }
                        }
                    }else if(len > 2){
                        if(eq && dp[i+1][j-1]){
                            dp[i][j] = true;
                            if(len > maxLen){
                                ll = i;
                                rr = j;
                                maxLen = len;
                            }
                        }
                    }
                }
            }
        }

        return s.substring(ll, rr+1);
    }
}
